Skip to content

Studio: Keep whole attachments available after chat compaction - #19841

Merged
Lyxot merged 14 commits into
unslothai:mainfrom
NilayYadav:studio-restore-whole-attachments-after-compaction
Nov 27, 2026
Merged

Lyxot merged 14 commits into
unslothai:mainfrom
NilayYadav:studio-restore-whole-attachments-after-compaction

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

When a local GGUF chat compacts, Studio selects attachment passages using the history before compaction. It can fall back to ranked excerpts even when the full attachment would fit in the prompt after older turns are removed.

Retry whole-document retrieval against the fitted prompt after compaction removes turns. Use the full attachment when it fits the prompt budget, with the ranked excerpts as a fallback. The document source card shows the passages that actually went to the model.

Before and after

Both chats compact with the same attached file. After the fix, the source card lists both passages and the answer quotes both headings.

Before, the answer gives the wrong headings. After, both document passages are retrieved and the answer gives Orchard Lantern and Harbor Compass.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T08:15:03.065215Z 05ff9df Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aace68f1b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: f6ee28f68b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…ent retries

Budget the post-compaction whole-document retry against the compaction headroom and the conversation-recall reserve on every fit, including boundary replays, so the next tool round and the recall that follows the fit still fit. Cap that fit's recall below the headroom line only when the document grew the prompt, skip the retry when audio or video parts are present (every fit leaves them uncounted), and keep a forced search_knowledge_base choice forced. Drop the trim_target field the rolling fit no longer needs to report.
@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Pushed a follow-up commit (cf2dca7) and merged the latest main.

What changed in the whole-document retry:

  • Budget on every fit, including boundary replays: the retry now always leaves the compaction headroom plus, when recall will run, the conversation-recall reserve free. Before, a replayed boundary let the document fill the prompt to the brim, so the next tool round compacted again and could evict the document.
  • Recall cap: the recall right after that fit is capped below the headroom line, but only when the document made the prompt larger. When it didn't, recall keeps its usual budget.
  • Audio/video: the retry is skipped when the conversation carries audio or video parts. The fit's token count leaves those uncounted, so the room they take isn't actually free.
  • Forced search_knowledge_base: a forced search_knowledge_base choice stays forced. The fit sized the prompt for that single tool.
  • fit_rolling_context: dropped the trim_target field it no longer needs to report.
  • Tests: added rows for replay headroom, recall reserve and cap, audio, and the forced tool list, plus a real-path test of whole_document_only in test_rag_whole_document.py.

@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 68d867ea26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The enabled-tool gate is now built by a helper the tool loop shares with the whole-document retry, so the source check anchors on that helper; it is still fed the sanitized catalog.
@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Pushed 4adc980. It updates test_gguf_execution_gate_is_built_from_the_sanitized_catalog, which failed in CI because the enabled-tool gate moved into the _round_tool_names helper. The test now anchors on that helper and on its call site, which passes the sanitized safe_tools. The other CI failure, tests/test_source_read_encoding.py, comes from tests/test_gguf_load_trainable.py on main (#19889) and is unrelated to this PR.

@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 4adc9801e8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Review summary for head 4adc9801e86.

Codex: converged. "Didn't find any major issues" on 4adc9801e8: #19841 (comment)

CI (staging copy, same head, based on current main, Lyxot#312): 40 passed, including Backend CI, GGUF inference smoke, Chat UI, API, and the Mac and Windows Studio jobs. Two failures, both from main and not from this PR, which only touches studio/backend:

What changed during review:

  • Retry budget: the whole-document retry keeps the compaction headroom, plus the recall reserve when recall runs, on every fit, including boundary replays.
  • Recall cap: the recall right after that fit is capped below the headroom line, but only when the document grew the prompt.
  • Audio/video: the retry is skipped when the conversation has audio or video parts, since the fit doesn't count them.
  • Forced tool choice: a forced search_knowledge_base choice stays forced.
  • fit_rolling_context: no longer reports trim_target.
  • Tests: new rows in test_llama_cpp_tool_loop.py, a real-path whole_document_only test, and the 7066 gate check re-anchored on _round_tool_names.

…ent retries

A checkpoint fit reserves nothing for recall, and the recall right after an accepted document is already capped below the headroom line, so subtracting the reserve on checkpoint fits only refused documents that fit (at 8k the accepted prompt dropped from 4,608 to 2,560 tokens).
The tool loop prices the request for the shared KV cache at the top of each round, before the fit. An accepted whole-document retry grows the prompt after that point, so re-run the same recost hook before the payload is sent.
…dling

Only tool_choice="required" is satisfied by an accepted document, which leaves the round's tool catalogue unchanged and only switches the choice to auto, so drop the candidate catalogue, the document_satisfies_choice parameter and the _round_tool_names helper (restoring the original execution-gate code and its source check). Recost admission only when the document grew the prompt.
…mission priced

Admission prices the request on the unfitted prompt at the top of the round, and a refused recost is not acted on. So instead of recosting, accept a whole document that grows the fitted prompt only up to the unfitted prompt's size; automatic compaction always starts above that line, so this binds only when a short chat is compacted by hand.
… unfitted prompt

On a hand-compacted short chat the recall that follows the fit could still push an accepted whole document past the prompt admission priced; cap that recall under the same ceiling as the document.
@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Pushed review fixes on top of 4adc980 (fast-forward, now 05ff9df):

  • The retry holds back the recall reserve only on rolling fits. A checkpoint fit keeps no reserve, so subtracting it there cut the accepted whole-document prompt from 4,608 to 2,560 tokens at 8k.
  • A document that grows the fitted prompt is accepted only up to the unfitted prompt's size, which admission already priced. Recall after such a document is capped at min(headroom line, unfitted prompt) minus the sent prompt. Automatic compaction always starts above the headroom line, so this binds only for a short chat compacted by hand.
  • The ceilings and the recall cap count the transcript link that archiving adds to the carried-forward block. Growth is still judged against the fit on the same uncounted basis, so a document no larger than the excerpts keeps the full recall budget.
  • The recost-after-acceptance attempt is removed: the route ignores a refused recost, and the bound above replaces it. The tool-choice handling is simplified back to main's catalogue: only tool_choice="required" is satisfied by the document search.
  • Tests: the parametrized whole-document test covers the reserve, the manual replay, the transcript link and the recall budgets. A new row checks that the MCP card still arrives when Docs is on and nothing compacts.

@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 05ff9df589

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Codex converged on 05ff9df: #19841 (comment)

CI (staging run on the same head and current main, since upstream runners were queued): 14 jobs passed. Three failed, and none of them comes from this branch:

What changed in this workflow: see #19841 (comment). In short, the whole-document retry is held to the fit's own target (headroom, plus the recall reserve on rolling fits) and to the size admission already priced, recall and the transcript link are counted inside that bound, the tool-choice handling matches main again, and there are tests for each.

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The whole-document retry after compaction stays within the fit's target and the size admission already priced; Codex and staging CI are clean apart from failures that also happen on main.

@Lyxot
Lyxot merged commit d7b0099 into unslothai:main Nov 27, 2026
39 of 44 checks passed
oobabooga added a commit to Imagineer99/unsloth that referenced this pull request Nov 27, 2026
Match the whole-document retry from unslothai#19841 by message source, so an
excerpt search call that argument compaction replays as a copy is
replaced with its result instead of being left as an unanswered call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants